/**
 * 分享JS
 */
$(document).ready(function() {
	$("#share").submit(function() {
				var formDom = $("#share").get(0);
				formDom.action = "./share_share_share.htm";
				formDom.method = "post";
	});
	
	$("#continue").click(function(){
		if(com.tietiao.Validator.isEmptyStr($("#hiddenPicPath").attr("value"))) {
			com.tietiao.timeAlert('<font color="red">请上传商品图片！</font>',2,true);
			return;
		}
		$("#shareArea").show();
	});
	
	$("#publish").click(function() {
		
		if (com.tietiao.Validator.isEmptyStr($("#hiddenPicPath").attr("value"))) {
			com.tietiao.timeAlert('<font color="red">请上传商品图片！</font>',2,true);
			return;
		}
		if (com.tietiao.Validator.isEmptyStr($("#commodityTitle").val())) {
			com.tietiao.timeAlert('<font color="red">请填写商品标题！</font>',1,true);
			$('#commodityTitle')[0].focus();
			return;
		}
		if (com.tietiao.Validator.isEmptyStr($("#commodity_lables").val())) {
			com.tietiao.timeAlert('<font color="red">请添加标签！</font>',1,true);
			$('#commodity_lables')[0].focus();
			return;
		}
		
		
		
		if (com.tietiao.Validator.isEmptyStr($("#favoriteId").val())) {
			com.tietiao.timeAlert('<font color="red">请选择我的收藏夹！</font>',1,true);
			return;
		}
		
		$('#commodityPic').remove();
		$("#share").submit();
	});

	$("#commodityPic").change(function() {
				ajaxFileUpload();
	 });
});

$.dialog.tips = function (content, followId) {
    return $.dialog({
        id: 'Tips',
        title: false,
        cancel: false,
        fixed: true,
        lock: true
    })
    .content('<div style="padding: 0 1em;">' + content + '</div>')
};

/**
 * 选中标签
 * 
 * @param {string}
 *            obj 对象
 * @param {string}
 *            lable lable标签
 */
function selectLable(aId, lable) {
	var lables = $("#commodity_lables").attr("value");
	if(lables.split(' ').length >= 3) {
		$.dialog.alert('最多填写三个标签!');
		return;
	}
	if(com.tietiao.Validator.isEmptyStr(lables)) {
		$("#commodity_lables").attr("value",lable);
	}
	else {
		$("#commodity_lables").attr("value",$("#commodity_lables").attr("value") + " " + lable);
	}
	
	$("#" + aId).addClass("added");
}

/**
 * Ajax上传方法
 */
function ajaxFileUpload() {
	var fileName = $('#commodityPic').val();
	if (!com.tietiao.Validator.checkPicSuffix(fileName)) {
		$.dialog.alert('请上传jpg|png|gif|bmp格式的图片!');
		return;
	}
	
	$.dialog.tips('正在上传图片，请稍候......','#user_commodity_image');
	$.ajaxFileUpload({
				url : './share_share_upload.htm',
				secureuri : false,
				fileElementId : 'commodityPic',
				dataType : 'json',
				success : function(data, status) {
					if (data.flag == 'true') {
						$('#hiddenPicPath').attr('value',
								data.imgPath);
						var img = '<img  src="' + data.imgRelPath + '" />';
						$('#publish_pic_but').html('重新上传');
						$('#user_commodity_image').get(0).innerHTML = img;
						
						$('#commodityPic').attr('value',
								'');
					} else {
						$.dialog.alert('图片大小或则图片分辨率不合格!');
					}
							
				},
				error : function(data, status, e) {
					$.dialog.alert('网络出现问题,请稍候...');
				},
				complete : function() {
					$('#jUploadFramecommodityPic').remove();
					$('#jUploadFormcommodityPic').remove();
					$("#commodityPic").change(function() {
								ajaxFileUpload();
					});
					$.dialog.list['Tips'].close();					
				}
			});
}

function loading() {
	$("#loading").ajaxStart(function() {
				$(this).show();
			}).ajaxComplete(function() {
				$(this).hide();
	});
}
/**
 * 选择收藏夹
 * @param {String} favoriteId
 */
function selectFavorite(favoriteId) {
	$('#myfavorite_div a').removeClass('added');
	$('#favorite_' + favoriteId).addClass('added');
	$('#favoriteId').attr('value',favoriteId);
}

// 添加收藏夹
function addFav() {
	var favoriteNa = $('#new-fav').val();
	if(com.tietiao.Validator.isEmptyStr(favoriteNa)) {
		return ;
	}
	$.post('common_common_addFav.htm',{favoriteName:favoriteNa},function(data) {
		var msg = eval('(' + data + ')');
		if(msg.suc == 'true') {
			$('#myfavorite_div').append('<a id="favorite_' + msg.msg +'" href="javascript:selectFavorite('+ msg.msg +');">'+ $('#new-fav').val() +'</a>');
			 $('#new-fav').val('');
		}
		else {
			com.tietiao.timeAlert(msg.msg,1,true);
		}
	});
}
